// pages/user/profile/edit/index.js
import api from '../../../../api/index'
import constant from '../../../../utils/constant'

const app = getApp();


Page({

  /**
   * 页面的初始数据
   */
  data: {
    indicatorStyle: `height: ${Math.round(app.globalData.screenHeight/(750/40))}px;`,
    act: '',
    tabCur: '',
    scope: 0,
    heightRange: constant.HEIGHT_RANGE(),
    weightRange: constant.WEIGHT_RANGE(),
    ageRange: constant.AGE_RANGE(),
    wEduRange: constant.W_EDU_RANGE(),
    wIncomeRange: constant.W_INCOME_RANGE(),
    areas: ['320000','320100','320104'],
    areaList: [],
    photoLen: 0,
    user: { },
    preAboutHer: '',
    aboutMe: '',
    aboutYou: '',
    interest: ''
  },





  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this
    wx.getStorage({
      key: 'profileEdit',
      success: function (res) {
        that.setData({
          act: res.data.act
        })
      }
    })

    api.common.getAreas().then(res=>{
      this.setData({
        areaList: res
      })
    })


    api.user.getPhotosCount().then(res=>{
      this.setData({
        photoLen: res
      })
    })
    api.user.getProfile().then(res=>{
      if(!wx.$isNull(res.lifeArea)){
        let area = [res.lifeProvince, res.lifeCity, res.lifeArea]
        res.areas = area
      }
      let WAreas = []
      if(wx.$isNull(res.WProvince)){
        WAreas.push('不限')
      }else{
        WAreas.push(res.WProvince)
      }
      if(wx.$isNull(res.WCity)){
        WAreas.push('不限')
      }else{
        WAreas.push(res.WCity)
      }
      res.WAreas = WAreas
      let WAge = []
      WAge.push(res.WAgeMin)
      WAge.push(res.WAgeMax)
      res.WAge = WAge
      let WHeight = []
      WHeight.push(res.WHeightMin)
      WHeight.push(res.WHeightMax)
      res.WHeight = WHeight

      if(!wx.$isNull(res.WCompany)){
        res.WCompany = res.WCompany.split(',')
      }
      if(!wx.$isNull(res.WMarital)){
        res.WMarital = res.WMarital.split(',')
      }

      
      this.setData({
        user: res,
        aboutMe: res.aboutMe,
        aboutYou: res.aboutYou,
        interest: res.interest
      })
      console.log(this.data)
      this.handleScope(that)

    })
  },

  handleScope(that){
    let i = 0
    if(this.data.photoLen > 0){   //1
      i = i+1
    }
    if(!wx.$isNull(this.data.user.height)){  //2
      i = i+1
    }
    if(!wx.$isNull(this.data.user.weight)){ //3
      i = i+1
    }
    if(!wx.$isNull(this.data.user.areas)){ //4
      i = i+1
    }
    if(!wx.$isNull(this.data.user.school)){ //5
      i = i+1
    }
    if(!wx.$isNull(this.data.user.schoolType)){ //6
      i = i+1
    }
    if(!wx.$isNull(this.data.user.education)){ //7
      i = i+1
    }
    if(!wx.$isNull(this.data.user.personality)){ //8
      i = i+1
    }
    if(!wx.$isNull(this.data.user.professionType)){ //9
      i = i+1
    }
    if(!wx.$isNull(this.data.user.companyType)){ //10
      i = i+1
    }
    if(!wx.$isNull(this.data.user.company)){ //11
      i = i+1
    }
    if(!wx.$isNull(this.data.user.workTime)){ //12
      i = i+1
    }
    if(!wx.$isNull(this.data.user.income)){ //13
      i = i+1
    }
    if(!wx.$isNull(this.data.user.house)){ //14
      i = i+1
    }
    if(!wx.$isNull(this.data.user.car)){ //15
      i = i+1
    }
    if(!wx.$isNull(this.data.user.nationality)){ //16
      i = i+1
    }
    if(!wx.$isNull(this.data.user.belief)){ //17
      i = i+1
    }
    if(!wx.$isNull(this.data.user.blood)){  //18
      i = i+1
    }
    if(!wx.$isNull(this.data.user.smoke)){ //19
      i = i+1
    }
    if(!wx.$isNull(this.data.user.maritalType)){ //20
      i = i+1
    }
    if(!wx.$isNull(this.data.user.maritalPlan)){ //21
      i = i+1
    }
    if(!wx.$isNull(this.data.user.wantChildren)){ //22
      i = i+1
    }
    if(!wx.$isNull(this.data.user.wantChildren)){ //23
      i = i+1
    }
    if(!wx.$isNull(this.data.user.wantChildren)){ //24
      i = i+1
    }
    if(!wx.$isNull(this.data.user.wantChildren)){ //25
      i = i+1
    }
    if(this.data.user.hasOwnProperty('WProvince') || this.data.user.hasOwnProperty('WCity')){ //26
      i = i+1
    }
    if(this.data.user.hasOwnProperty('WAgeMin') || this.data.user.hasOwnProperty('WAgeMax') ){ //27
      i = i+1
    }
    if(this.data.user.hasOwnProperty('WHeightMin') || this.data.user.hasOwnProperty('WHeightMin') ){ //28
      i = i+1
    }
    if(this.data.user.hasOwnProperty('WEducation')){ //29
      i = i+1
    }
    if(this.data.user.hasOwnProperty('WCompany') ){ //30
      i = i+1
    }
    if(this.data.user.hasOwnProperty('WIncome') ){ //31
      i = i+1
    }
    if(this.data.user.hasOwnProperty('WMarital') ){ //32
      i = i+1
    }
    if(!wx.$isNull(this.data.user.aboutMe)){ //33
      i = i+1
    }
    if(!wx.$isNull(this.data.user.interest)){ //34
      i = i+1
    }
    if(!wx.$isNull(this.data.user.aboutYou)){ //35
      i = i+1
    }

    let sp = Math.trunc((i/35)*100 )
    setTimeout(function() {
      that.setData({
        scope: sp,
        loading: true
      })
    }, 500)
  },
   async handleTxt(){
    let user = this.data.user, eduList = this.data.wEduRange, incomeList = this.data.wIncomeRange, areaList = this.data.areaList, compList = [], marList = []
    let ageTxt = '', heightTxt = '', eduTxt = '', incomeTxt = '', companyTxt= '', wMarTxt = '', areaTxt = '',sexTxt = '的人'
    await api.common.getDictData('user_company_type').then(res=>{
      compList = res
    })
    await api.common.getDictData('user_marital_type').then(res=>{
      marList = res
    })
    

    const date = new Date()
    let y = date.getFullYear()
    if(wx.$isNull(user.WAgeMin) && wx.$isNull(user.WAgeMax)){
      ageTxt = "年龄: 不限，"
    }else if(wx.$isNull(user.WAgeMin) && !wx.$isNull(user.WAgeMax)){
      let oy1 = (y-user.WAgeMax).toString()
      let y1 = oy1.slice(2)
      ageTxt =  '年龄: '+ y1 + '前，'
    }else if(!wx.$isNull(user.WAgeMin) && wx.$isNull(user.WAgeMax)){
      let oy2 = (y-user.WAgeMin).toString()
      let y2 = oy2.slice(2)
      ageTxt =  '年龄: '+ y2 + '后，'
    }else{
      let oy2 = (y-user.WAgeMin).toString()
      let oy1 = (y-user.WAgeMax).toString()
      let y2 = oy2.slice(2)
      let y1 = oy1.slice(2)
      ageTxt = '年龄在'+ y1 + '-' + y2 + '年之间，'
    }
    if(wx.$isNull(user.WHeightMin) && wx.$isNull(user.WHeightMax)){
      heightTxt = " 身高: 不限，"
    }else if(wx.$isNull(user.WHeightMin) && !wx.$isNull(user.WHeightMax)){
      let y2 = (user.WHeightMax/100).toString()
      y2 = y2.slice(0,1) +'米'+y2.slice(2)
      heightTxt ='身高: '+ y2 +'以下，'
    }else if(!wx.$isNull(user.WHeightMin) && wx.$isNull(user.WHeightMax)){
      let y1 = (user.WHeightMin/100).toString()
      y1 = y1.slice(0,1) +'米'+y1.slice(2)
      heightTxt = '身高: '+y1+'以上，'
    }else{
      let y1 = (user.WHeightMin/100).toString()
      let y2 = (user.WHeightMax/100).toString()
      y1 = y1.slice(0,1) +'米'+y1.slice(2)
      y2 = y2.slice(0,1) +'米'+y2.slice(2)
      heightTxt = '身高在'+ y1 +'-' + y2+'，'
    }
    if(!wx.$isNull(user.WEducation)){
      for(const i in eduList){
        if(user.WEducation == eduList[i].val){
          eduTxt = eduList[i].name + '学历，'
          break
        }
      }
    }
    if(!wx.$isNull(user.WCompany)){
      let wc = []
      for(const i in compList){
        for(const c in user.WCompany){
         
          if(user.WCompany[c] == compList[i].dictValue){
            wc.push(compList[i].dictLabel)
          }
        }
      }
      companyTxt = '在' + wc.join('、') + '工作，'
    }
    if(!wx.$isNull(user.WIncome)){
      for(const i in incomeList){
        if(user.WIncome == incomeList[i].val){
          incomeTxt = '年收入'+incomeList[i].name+'，'
          break
        }
      }
    }
    if(!wx.$isNull(user.WMarital)){
      let wm = []
      for(const i in marList){
        for(const c in user.WMarital){
          if(user.WMarital[c] == marList[i].dictValue){
            wm.push(marList[i].dictLabel)
          }
        }
      }
      wMarTxt = wm.join('、')
    }
    if(!wx.$isNull(user.WProvince)){
      areaTxt = " 家乡是：" + user.WProvince
    }
    if(wx.$isNull(user.WCity)){
      areaTxt = areaTxt + '内'
    }else{
      areaTxt = areaTxt + user.WCity
    }

    let u = wx.getStorageSync(constant.USER_INFO)
    if(u.sex == '男'){
      sexTxt = '的女生'
    }else if(u.sex == '女'){
      sexTxt = '的男生'
    }
    let txt = '我在寻找: ' + ageTxt + heightTxt + eduTxt + incomeTxt + companyTxt + wMarTxt
    if(wx.$isNull(areaTxt)){
      return txt.slice(0, -1) + sexTxt
    }else{
      return txt + areaTxt + sexTxt
    }

  },
  tabSelect(e) {
    this.setData({
      tabCur: e.currentTarget.dataset.tab
    })
    if(e.currentTarget.dataset.tab == 2){
      this.handleTxt().then(res=>{
        this.setData({
          preAboutHer: res
        })
      })

    }
  },
  onPicker(e){
    let inx = e.currentTarget.dataset.index
    let v = e.detail
    let data = {}
    let user = this.data.user
    switch(inx){
      case 'height': 
        data = {
          height: v.val
        }
        user.height = v.val
        break
      case 'weight':
        data = {
          weight: v.val
        }
        user.weight = v.val
        break
      case 'areas':
        console.log(v)
        data = {
          lifeProvince: v.name[0],
          lifeCity: v.name[1],
          lifeArea: v.name[2]
        }
        if(wx.$isNull(v.val[2])){
          wx.$msg('请选择居住地')
        }else{
          user.areas = v.name
        }
        break
      case 'school':
        data = {
          school: v.value
        }
        user.school = v.value
        break
      case 'schoolType':
        data={
          schoolType: v.val
        }
        user.schoolType = v.val
        break
      case 'education':
        data={
          education: v.val
        }
        user.education = v.val
        break
      case 'personality':
        data={
          personality: v.val
        }
        user.personality = v.val
        break
      case 'professionType':
        data = {
          professionType: v.val
        }
        user.professionType = v.val
        break
      case 'relation':
        data = {
          relation: v.val
        }
        user.relation = v.val
        break
      case 'companyType':
        data = {
          companyType: v.val
        }
        user.companyType = v.val
        break  
      case 'company':
        data = {
          company: v.value
        }
        user.company = v.value
        break
      case 'workTime':
        data = {
          workTime: v.val
        }
        user.workTime = v
        break
      case 'income':
        data = {
          income: v.val
        }
        user.income = v.val
        break
      case 'house':
        data = {
          house: v.val
        }
        user.house = v.val
        break
      case 'car':
        data = {
          car: v.val
        }
        user.car = v.val
        break
      case 'nationality':
        data = {
          nationality: v.val
        }
        user.nationality = v.val
        break
      case 'belief':
        data = {
          belief: v.val
        }
        user.belief = v.val
        break
      case 'blood':
        data = {
          blood: v.val
        }
        user.blood = v.val
        break
      case 'smoke':
        data = {
          smoke: v.val
        }
        user.smoke = v.val
        break
      case 'drink':
        data = {
          drink: v.val
        }
        user.drink = v.val
        break
      case 'maritalType':
        data = {
          maritalType: v.val
        }
        user.maritalType = v.val
        break
      case 'childInfo':
        data = {
          childInfo: v.value
        }
        user.childInfo = v.value
        break
      case 'maritalPlan':
        data = {
          maritalPlan: v.val
        }
        user.maritalPlan = v
        break
      case 'wantChildren':
        data = {
          wantChildren: v.val
        }
        user.wantChildren = v.val
        break
    }
    api.user.setProfile(data).then(res=>{
      this.setData({
        user: user
      })
    })
    
  },
  onWPicker(e){
    let inx = e.currentTarget.dataset.index
    let v = e.detail, data = {}
    let user = this.data.user
    if(inx == 'wArea'){
      let wArea = []
      if(!wx.$isNull(v.val[0]) && !wx.$isNull(v.val[1])){
        data = {
          wProvince: v.name[0],
          wCity: v.name[1]
        }
        wArea = [v.name[0],v.name[1]]
      }else if(wx.$isNull(v.val[0]) && !wx.$isNull(v.val[1])){
        data = {
          wProvince: '',
          wCity: v.name[1]
        }
        wArea = ['不限',v.name[1]]
      }else if(!wx.$isNull(v.val[0]) && wx.$isNull(v.val[1])){
        data = {
          wProvince: v.name[0],
          wCity: ''
        }
        wArea = [v.name[0],'不限']
      }else{
        data = {
          wProvince: '',
          wCity: ''
        }
        wArea = ['不限','不限']
      }
      let areas = []
      areas.push(data.wProvince)
      areas.push(data.wCity)
      user.WAreas = wArea
    }else if(inx == 'wAge'){
      if(!wx.$isNull(v.val[0]) && !wx.$isNull(v.val[1])){
        data = {
          wAgeMin: v.val[0],
          wAgeMax: v.val[1]
        }
      }else if(wx.$isNull(v.val[0]) && !wx.$isNull(v.val[1])){
        data = {
          wAgeMin: '',
          wAgeMax: v.val[1]
        }
      }else if(!wx.$isNull(v.val[0]) && wx.$isNull(v.val[1])){
        data = {
          wAgeMin: v.val[0],
          wAgeMax: ''
        }
      }else{
        data = {
          wAgeMin: '',
          wAgeMax: ''
        }
      }
      let ages = []
      ages.push(data.wAgeMin)
      ages.push(data.wAgeMax)
      user.WAge = ages
    }else if(inx == 'wheight'){
      if(!wx.$isNull(v.val[0]) && !wx.$isNull(v.val[1])){
        data = {
          wHeightMin: v.val[0],
          wHeightMax: v.val[1]
        }
      }else if(wx.$isNull(v.val[0]) && !wx.$isNull(v.val[1])){
        data = {
          wHeightMin: '',
          wHeightMax: v.val[1]
        }
      }else if(!wx.$isNull(v.val[0]) && wx.$isNull(v.val[1])){
        data = {
          wHeightMin: v.val[0],
          wHeightMax: ''
        }
      }else{
        data = {
          wHeightMin: '',
          wHeightMax: ''
        }
      }
      let heights = []
      heights.push(data.wHeightMin)
      heights.push(data.wHeightMax)
      user.WHeight = heights
    }else if(inx == 'wEducation'){
      if(wx.$isNull(v.val)){
        data = {
          wEducation: ''
        }
      }else{
        data = {
          wEducation: v.val
        }
      }
      user.WEducation = data.wEducation
    }else if(inx == 'wCompany'){
      if(wx.$isNull(v.val)){
        data={
          wCompany: ''
        }
      }else{
        data={
          wCompany: v.val.toString()
        }
      }
      user.WCompany = data.wCompany.split(',')
    }else if(inx == 'wIncome'){
      if(wx.$isNull(v.val)){
        data={
          wIncome: ''
        }
      }else{
        data={
          wIncome: v.val
        }
      }
      user.WIncome = data.wIncome
    }else if(inx == 'wMarital'){
      if(wx.$isNull(v.val)){
        data={
          wMarital: ''
        }
      }else{
        data={
          wMarital: v.val.toString()
        }
      }
      user.WMarital = data.wMarital.split(',')
    }

    api.user.setProfile(data).then(res=>{
      this.setData({
        user: user
      })
    })
  },
  onInput(e){
    let inx = e.currentTarget.dataset.index
    let v = e.detail
    if(inx == 'me'){
      this.setData({
        aboutMe: v.value
      })
    }else if(inx == 'you'){
      this.setData({
        aboutHer: v.value
      })
    }else if(inx == 'interest'){
      this.setData({
        interest: v.value
      })
    }
  },
  onOK(){
    const data = {
      aboutMe: this.data.aboutMe,
      aboutYou: this.data.aboutYou,
      interest: this.data.interest,
      status: '0'
    }
    api.user.setProfile(data).then(res=>{
      wx.$msg('修改完成,等待审核')
    })
  }


})